package cn.org.donald.controller.game;


import cn.org.donald.framework.constant.InfoConstant;
import cn.org.donald.framework.handler.ServiceException;
import cn.org.donald.framework.pojo.dto.IdsDto;
import cn.org.donald.framework.pojo.dto.PageDto;
import cn.org.donald.framework.pojo.vo.HttpResult;
import cn.org.donald.framework.pojo.vo.TableResult;
import cn.org.donald.framework.validate.Insert;
import cn.org.donald.framework.validate.Update;
import cn.org.donald.pojo.dto.game.FeatureDto;
import cn.org.donald.pojo.entity.game.Feature;
import cn.org.donald.service.game.FeatureService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import cn.org.donald.framework.base.BaseController;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Donald
 * @since 2020-12-12
 */
@RestController
@RequestMapping("/game/feature")
@Api(value = "游戏特性相关接口")
public class FeatureController extends BaseController {

    @Reference(version = "1.0.0", group = "donald")
    private FeatureService featureService;


    @PostMapping("/add")
    @ApiOperation(value = "新增特性")
    @RequiresPermissions("feature:add:post")
    public HttpResult add(@Validated @RequestBody FeatureDto featureDto,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new ServiceException(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        return featureService.add(featureDto) ? toResultSuccess(InfoConstant.ADD_SUCCESS) : toResultFail(InfoConstant.ADD_FAIL);
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页查询特性列表")
    @RequiresPermissions("feature:page:post")
    public TableResult listPage(@Validated({Insert.class}) @RequestBody PageDto<Feature> pageDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new ServiceException(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        IPage<Feature> page = featureService.findPage(pageDto);
        return toTableResult(page);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "修改特性")
    @RequiresPermissions("feature:edit:put")
    public HttpResult edit(@Validated({Update.class}) @RequestBody FeatureDto featureDto,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new ServiceException(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        return featureService.editById(featureDto)?toResultSuccess(InfoConstant.EDIT_SUCCESS):toResultFail(InfoConstant.EDIT_FAIL,InfoConstant.ID_IS_NOT_EXIST);
    }


    @GetMapping("/detail/{id}")
    @ApiOperation(value = "根据id查询特性详情")
    public HttpResult getOneById(@PathVariable Long id){
        Feature feature = featureService.findOneById(id);
        return toResultSuccess(feature);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除特性")
    @RequiresPermissions("feature:delete:delete")
    public HttpResult deleteByIds(@RequestBody IdsDto idsDto){
        return featureService.deleteByIds(idsDto) ? toResultSuccess(InfoConstant.DELETE_SUCCESS) : toResultFail(InfoConstant.DELETE_FAIL,InfoConstant.ID_IS_NOT_EXIST);
    }

}

